package iotwisp.base.tree;

import java.util.ArrayList;
import java.util.List;

import iotwisp.base.cqrs.BaseQO;
import iotwisp.base.orm.query.QueryCondition;
import iotwisp.base.orm.query.QueryConditionType;
import lombok.Data;
import lombok.EqualsAndHashCode;

@SuppressWarnings("serial")
@Data
@EqualsAndHashCode(callSuper = false)
public class TreeDataBaseQO extends BaseQO<String> {

    @QueryCondition(name = "treeData.level")
    private Integer treeDataLevel;

    @QueryCondition(name = "treeData.level", type = QueryConditionType.IN)
    private List<Integer> levels;

    @QueryCondition(name = "treeData.level", type = QueryConditionType.ORDER)
    private Integer orderByLevel;

    @QueryCondition(name = "treeData.leftValue", type = QueryConditionType.ORDER)
    private Integer orderByLeftValue;

    @QueryCondition(name = "treeData.treeId")
    private String treeDataTreeId;

    @QueryCondition(name = "treeData.treeId", type = QueryConditionType.IN)
    private List<String> treeIds;

    @QueryCondition(name = "treeData.leftValue")
    private Integer treeDataLeftValue;

    @QueryCondition(name = "treeData.rightValue")
    private Integer treeDataRightValue;

    @QueryCondition(name = "treeData.parentId")
    private String treeDataParentId;

    @QueryCondition(name = "treeData.leftValue", type = QueryConditionType.BETWEEN)
    private List<Integer> leftValueBetween;

    @QueryCondition(name = "treeData.rightValue", type = QueryConditionType.BETWEEN)
    private List<Integer> rightValueBetween;

    /**
     * 库中左值大于等于输入值
     */
    @QueryCondition(name = "treeData.leftValue", type = QueryConditionType.GE)
    private Integer geLeftValue;

    /**
     * 库中左值大于输入值
     */
    @QueryCondition(name = "treeData.leftValue", type = QueryConditionType.GT)
    private Integer gtLeftValue;

    /**
     * 库中左值小于等于输入值
     */
    @QueryCondition(name = "treeData.leftValue", type = QueryConditionType.LE)
    private Integer leLeftValue;

    /**
     * 库中左值小于输入值
     */
    @QueryCondition(name = "treeData.leftValue", type = QueryConditionType.LT)
    private Integer ltLeftValue;

    /**
     * 库中右值小于等于输入值
     */
    @QueryCondition(name = "treeData.rightValue", type = QueryConditionType.LE)
    private Integer leRightValue;

    /**
     * 库中右值小于输入值
     */
    @QueryCondition(name = "treeData.rightValue", type = QueryConditionType.LT)
    private Integer ltRightValue;

    /**
     * 库中右值大于等于输入值
     */
    @QueryCondition(name = "treeData.rightValue", type = QueryConditionType.GE)
    private Integer geRightValue;

    /**
     * 库中右值大于输入值
     */
    @QueryCondition(name = "treeData.rightValue", type = QueryConditionType.GT)
    private Integer gtRightValue;

    public void queryPathBetween2Node(EMTreeData root, EMTreeData target) {
        setLeftValueBetween(new ArrayList<Integer>());
        getLeftValueBetween().set(0, root.getLeftValue());
        getLeftValueBetween().set(1, target.getLeftValue());

        setRightValueBetween(new ArrayList<Integer>());
        getRightValueBetween().set(0, target.getRightValue());
        getRightValueBetween().set(1, root.getRightValue());
    }

    /**
     * 以某个节点为根，查询整棵下级树节点
     *
     * @param root
     * @param includeRoot
     */
    public void queryAllChildrenTreeNode(EMTreeData root, boolean includeRoot) {
        if (includeRoot) {
            setGeLeftValue(root.getLeftValue());
            setLeRightValue(root.getRightValue());
        } else {
            setGtLeftValue(root.getLeftValue());
            setLtRightValue(root.getRightValue());
        }

    }

    /**
     * 以某个节点为根，查询下一级子节点
     *
     * @param root
     * @param includeRoot
     */
    public void queryNextLevelChildrenTreeNode(EMTreeData root) {
        setGtLeftValue(root.getLeftValue());
        setLtRightValue(root.getRightValue());
        setTreeDataLevel(root.getLevel() + 1);
    }

    /**
     * 查询某个节点所有上级树节点
     *
     * @param node
     * @param includeCurrent
     * @param rootOnTop      排序，从上到下
     */
    public void queryAllParentTreeNode(EMTreeData node, boolean includeCurrent, boolean rootOnTop) {
        if (includeCurrent) {
            setLeLeftValue(node.getLeftValue());
            setGeRightValue(node.getRightValue());
        } else {
            setLtLeftValue(node.getLeftValue());
            setGtRightValue(node.getRightValue());
        }

        if (rootOnTop) {
            setOrderByLeftValue(TreeDataBaseQO.ORDER_ASC);
        } else {
            setOrderByLeftValue(TreeDataBaseQO.ORDER_DESC);
        }
    }

    /**
     * 查询某个节点所有子节点中最右边的一个
     *
     * @param parentNodeId
     */
    public void queryRightEndNode(String parentNodeId) {

        setTreeDataParentId(parentNodeId);
        setMaxProperty("treeData.rightValue");
        setMaxValueProperty("treeDataRightValue");
    }
}
